import axios from 'axios'
import qs from "qs"

axios.interceptors.response.use(
    res => {
        if (
            res.data ==
            "<script type='text/javascript'>top.location.href = '/oauth2'</script>"
        ) {
            sessionStorage.clear()
            window.location.href = "http://union.hunnu.edu.cn/api/oauth2";
            return false;
        } else if (
            res.data.code == "fail" &&
            res.data.desc == "请先登录！"
        ) {
            sessionStorage.clear()
            window.location.href = "http://union.hunnu.edu.cn/api/oauth2";
            return false
        }
        return res
    },
    error => {
        return Promise.reject(error)
    }
)

function getp(url, params) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params
        }).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err.data)
        })
    })
}

function postp(url, params) {
    return new Promise((resolve, reject) => {
        axios({
            url: url,
            method: "post",
            params
        }).then(res => {
            resolve(res.data);
        })
            .catch(err => {
                reject(err.data)
            })
    });
}

function get(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            data: data
        })
            .then(res => {
                resolve(res.data)
            })
            .catch(err => {
                reject(err)
            })
    })
}

/* 封装 Promise的post请求 */
function post(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data)
            .then(res => {
                resolve(res.data)
            }, err => {
                reject(err)
            })
    })
}

function postDefault(url, data) {
    return new Promise((resolve, reject) => {
        axios({
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
            },
            method: "post",
            url: url,
            data,
        }).then((res) => {
            resolve(res.data);
        }, err => {
            reject(err)
        })
    })
}

function posts(url, params, data = {}) {
    return new Promise((resolve, reject) => {
        axios({
            method: "post",
            url: url,
            params,
            data: qs.stringify(data),
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
            }
        }).then((res) => {
            resolve(res.data);
        }, err => {
            reject(err)
        })
    })
}

function postAP(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios({
            method: "post",
            url: url,
            data: data,
            headers: {
                'Content-Type': 'multipart/form-data'
            },
        }).then((res) => {
            resolve(res.data);
        }, err => {
            reject(err)
        })
    })
}


export default {
    postp,
    post,
    getp,
    get,
    posts,
    postDefault,
    postAP
}